package com.qc.wintrax.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.qc.wintrax.BaseActivity;
import com.qc.wintrax.R;
import com.qc.wintrax.WTApplication;
import com.qc.wintrax.fragment.LocateFragment;
import com.qc.wintrax.fragment.EventFragment;
import com.qc.wintrax.fragment.MineFragment;
import com.qc.wintrax.fragment.SearchFragment;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class MainActivity extends BaseActivity {
    @Bind(R.id.fl_content)
    FrameLayout flContent;
    @Bind(R.id.id_tab_weixin_img)
    ImageButton idTabWeixinImg;
    @Bind(R.id.id_tab_weixin)
    LinearLayout idTabWeixin;
    @Bind(R.id.id_tab_address_img)
    ImageButton idTabAddressImg;
    @Bind(R.id.id_tab_address)
    LinearLayout idTabAddress;
    @Bind(R.id.id_tab_frd_img)
    ImageButton idTabFrdImg;
    @Bind(R.id.id_tab_frd)
    LinearLayout idTabFrd;
    @Bind(R.id.id_tab_settings_img)
    ImageButton idTabSettingsImg;
    @Bind(R.id.id_tab_settings)
    LinearLayout idTabSettings;
    @Bind(R.id.huodong_txt)
    TextView huodongTxt;
    @Bind(R.id.dingwei_txt)
    TextView dingweiTxt;
    @Bind(R.id.faxian_txt)
    TextView faxianTxt;
    @Bind(R.id.mine_txt)
    TextView mineTxt;
    @Bind(R.id.title_home)
    TextView titleHome;
    @Bind(R.id.bar)
    RelativeLayout bar;


    private Fragment homeFragment;
    private Fragment contactsFragment;
    private Fragment searchFragment;
    private Fragment mineFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initFragment(0);
    }

    private void initFragment(int index) {
        // 由于是引用了V4包下的Fragment，所以这里的管理器要用getSupportFragmentManager获取
        FragmentManager fragmentManager = getSupportFragmentManager();
        // 开启事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
//        transaction.setCustomAnimations(R.anim.fragment_slide_right_in, R.anim.fragment_slide_left_out,
//                R.anim.fragment_slide_left_in, R.anim.fragment_slide_right_out);
        // 隐藏所有Fragment
        hideFragment(transaction);
        switch (index) {
            case 0:
                if (homeFragment == null) {
                    homeFragment = new EventFragment();
                    transaction.add(R.id.fl_content, homeFragment);
                } else {
                    transaction.show(homeFragment);
                }
                break;
            case 1:
                if (contactsFragment == null) {
                    contactsFragment = new LocateFragment();
                    transaction.add(R.id.fl_content, contactsFragment);
                } else {
                    transaction.show(contactsFragment);
                }
                break;
            case 2:
                if (searchFragment == null) {
                    searchFragment = new SearchFragment();
                    transaction.add(R.id.fl_content, searchFragment);
                } else {
                    transaction.show(searchFragment);
                }
                break;
            case 3:
                if (mineFragment == null) {
                    mineFragment = new MineFragment();
                    transaction.add(R.id.fl_content, mineFragment);
                } else {
                    transaction.show(mineFragment);
                }
                break;
            default:
                break;
        }
        // 提交事务
        transaction.commit();
    }

    //隐藏Fragment
    private void hideFragment(FragmentTransaction transaction) {
        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (contactsFragment != null) {
            transaction.hide(contactsFragment);
        }
        if (searchFragment != null) {
            transaction.hide(searchFragment);
        }
        if (mineFragment != null) {
            transaction.hide(mineFragment);
        }
    }

    private void restartBotton() {
        // ImageView置为灰色
        idTabWeixinImg.setImageResource(R.mipmap.huodong);
        idTabAddressImg.setImageResource(R.mipmap.dingwei);
        idTabFrdImg.setImageResource(R.mipmap.faxian);
        idTabSettingsImg.setImageResource(R.mipmap.mine);
        // TextView置为白色
        huodongTxt.setTextColor(getResources().getColor(R.color.huise));
        dingweiTxt.setTextColor(getResources().getColor(R.color.huise));
        faxianTxt.setTextColor(getResources().getColor(R.color.huise));
        mineTxt.setTextColor(getResources().getColor(R.color.huise));
    }

    @OnClick({R.id.id_tab_weixin, R.id.id_tab_address, R.id.id_tab_frd, R.id.id_tab_settings})
    public void onClick(View view) {
        // 在每次点击后将所有的底部按钮(ImageView,TextView)颜色改为灰色，然后根据点击着色
        restartBotton();
        // ImageView和TetxView置为绿色，页面随之跳转
        switch (view.getId()) {
            case R.id.id_tab_weixin:
                idTabWeixinImg.setImageResource(R.mipmap.huodong2);
                huodongTxt.setTextColor(getResources().getColor(R.color.lvse2));
                titleHome.setText(getResources().getString(R.string.event_title));
                initFragment(0);
                break;
            case R.id.id_tab_address:
                idTabAddressImg.setImageResource(R.mipmap.dingwei2);
                dingweiTxt.setTextColor(getResources().getColor(R.color.lvse2));
                titleHome.setText(getResources().getString(R.string.location_title));
                initFragment(1);
                break;
            case R.id.id_tab_frd:
                idTabFrdImg.setImageResource(R.mipmap.faxian2);
                faxianTxt.setTextColor(getResources().getColor(R.color.lvse2));
                titleHome.setText(getResources().getString(R.string.find_title));
                initFragment(2);
                break;
            case R.id.id_tab_settings:
                idTabSettingsImg.setImageResource(R.mipmap.mine2);
                mineTxt.setTextColor(getResources().getColor(R.color.lvse2));
                titleHome.setText(getResources().getString(R.string.mine_title));
                initFragment(3);
                break;
            default:
                break;
        }
    }

    /**
     * 按返回键两次退出程序
     */
    private long mExitTime;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            {
                if ((System.currentTimeMillis() - mExitTime) > 2000) {
                    Toast.makeText(this, getResources().getString(R.string.hint_quit), Toast.LENGTH_SHORT).show();
                    mExitTime = System.currentTimeMillis();
                } else {
                    WTApplication.getInstance().finishAllActivity();
                }
            }
            return true;
        }
        //拦截MENU按钮点击事件，让他无任何操作
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
